package com.yjc.drygoods.order.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yjc.common.vo.ConsumeAttentionCountRelationVo;
import com.yjc.drygoods.order.entity.OrderEntity;
import com.yjc.drygoods.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 订单
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 23:20:12
 */
@Slf4j
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @PostMapping("/get/everyone/consumeTotal")
    public R getEveryoneConsumeTotal(@RequestBody List<Long> memberIds) {
        List<ConsumeAttentionCountRelationVo> res = orderService.getEveryoneConsumeTotal(memberIds);
        log.info("res:{}", res);
        return R.ok().setData(res);
    }

    @GetMapping("/getOrderInfo")
    public R getOrderInfoByOrderSn(@RequestParam("orderSn") String orderSn) {
        OrderEntity order = orderService.getOrderInfoByOrderSn(orderSn);
        return R.ok().setData(order);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @PostMapping("/listWithOrderItems")
    public R listWithOrderItems(@RequestBody Map<String, Object> params) {
        PageUtils page = orderService.listWithOrderItems(params);
        log.info("page:{}", page.toString());

        return R.ok().put("page", page);
    }

    /**
     * 获取所有订单兼旗下所有订单项
     */
    @GetMapping("/getAllOrder/with/orderItems")
    public R getAllOrderWithOrderItems() {
        List<OrderEntity> resList = orderService.getAllOrderWithOrderItems();
//        log.info("resList:{}", resList.toString());
        return R.ok().put("data", resList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order) {
        orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order) {
        orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
